<?php
namespace App\Service;
use App\Common\Utill\OperationLog;
use App\Models\Tasks;
use Illuminate\Support\Facades\Log;

class TasksService extends CommonService
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getListData($request,$num)
    {
        $accountAuthType = self::accountAuthType();
        $model = new Tasks();
        $query = $model::query();
        if($accountAuthType != 1){
            $websiteArr = WebsitesAdminService::getAdminWebsites();
            if($websiteArr){
                $query->whereIn('t.websites_id', $websiteArr);
            }else{
                $query->where('t.websites_id', -1);
            }
        }

        $query->from('pd_tasks as t');
        //名称
        $query->select('t.*','w.names as website_name','l.titles as topic_name');
        $query->leftJoin("pd_websites as w",'w.id','=','t.websites_id');
        $query->leftJoin("pd_websites_topic as l",'l.id','=','t.websites_topic_id');
        $query->where("t.is_del",1);
        $query->orderBy('t.id','desc');
        $result = $query->paginate($num)->toArray();
        foreach ($result['data'] as $key => $val) {
            $result['data'][$key]['plan_time_date'] = date("Y-m-d H:i:s",$val['plan_time']);
            $result['data'][$key]['status_name'] = $this->getStatusName($val['status']);
        }
        return  $this->success('SUCCESS',$result['total'],$result['data']);
    }

    public function getStatusName($status)
    {
        $arr = array(
            0=>'待开始',
            1=>'进行中',
            2=>'已完成'
        );
        return $arr[$status] ?? '';
    }


    /**
     * 添加数据保存
     * @return void
     */
    public function create($website_id,$websites_topic_id,$plan_time,$plan_numbers)
    {
        $plan_time = strtotime($plan_time);
        if(time()>$plan_time){
            return $this->error('必须大于当前时间');
        }
        $operationData = self::operationData($website_id);
        $model = new Tasks();
        $model->websites_id = $website_id;
        $model->websites_topic_id  = $websites_topic_id;
        $model->plan_numbers  = $plan_numbers;
        $model->plan_time  = $plan_time;
        $model->status  = 0;
        $model->admin_id  = $operationData['admin_id'];
        $model->operation_id  = $operationData['operation_id'];
        $model->created_at  = date("Y-m-d H:i:s");
        if(!$model->save()){
            return $this->error('添加失败！！！');
        }else{
            OperationLog::record(1,"任务-添加",$model->id,$model->toArray());
            return $this->success('添加成功');
        }
    }

    /**
     * 编辑
     * @param $data
     * @return void
     */
    public function update($id,$website_id,$websites_topic_id,$plan_time,$plan_numbers){
        $oldData = Tasks::query()->where('id',$id)->first()->toArray();
        $plan_time = strtotime($plan_time);
        $model = Tasks::findOrFail($id);
        if($model->status == 2){
            return $this->error('已完成状态，无法修改');
        }
        if($model->status == 1){
            if($plan_numbers < $model->plan_numbers){
                return $this->error('进行中状态，数量必须大于当前值');
            }
            if($website_id != $model->websites_id || $model->websites_topic_id != $websites_topic_id){
                return $this->error('进行中状态，不能修改网站或者栏目');
            }
            if($plan_time != $model->plan_time){
                return $this->error('进行中状态，不能修改时间');
            }
        }
        $model->websites_id = $website_id;
        $model->websites_topic_id  = $websites_topic_id;
        $model->plan_numbers  = $plan_numbers;
        $model->plan_time  = $plan_time;
        $model->updated_at  = date("Y-m-d H:i:s");
        if(!$model->save()){
            return $this->error('修改失败');
        }
        OperationLog::record(2,"任务-编辑",$id,$model->toArray(),$oldData);
        return $this->success("修改成功");
    }

    /**
     * 单个删除
     */
    public function delOne($id){
        $result = Tasks::findOrFail($id);
        $result->is_del = -1;
        $result->updated_at = date("Y-m-d H:i:s");
        if (!$result->save()) {
            return $this->error('删除失败');
        }
        OperationLog::record(3,"任务-删除",$id);
        return $this->success("删除成功");
    }

    /**
     * 批量删除
     * @return void
     */
    public function delAll($idArr)
    {
        if(!$idArr){
            return $this->error('删除内容不能为空');
        }
        Log::info("删除id:",$idArr);
        $delData['is_del'] = -1;
        $delData['updated_at'] = date("Y-m-d H:i:s");
        $result = Tasks::query()->whereIn("id",$idArr)->update($delData);
        if(!$result){
            return $this->error('删除失败');
        }
        OperationLog::record(3,"任务-删除",0,'','',implode(",",$idArr));
        return $this->success("删除成功");
    }


}
